package com.mvu.banana.internal.server;


import com.mvu.banana.domain.client.Gender;
import com.mvu.banana.domain.stub.Credential;
import com.mvu.banana.domain.stub.Profile;
import com.mvu.banana.internal.builder.CredentialBuilderStub;
import org.jasypt.util.password.BasicPasswordEncryptor;

import java.util.ArrayList;
import java.util.List;

import static com.mvu.banana.internal.server.RandomGenerator.randomString;

public class CredentialBuilder extends CredentialBuilderStub {

  private ProfileBuilder profileBuilder;

  public void init(){
    withUsername(randomString());
    withPassword("gw");
    withPermissions("some");
    profileBuilder = new ProfileBuilder();
  }

  public CredentialBuilder withPassword(String pwd){
    BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
    super.withPassword(passwordEncryptor.encryptPassword(pwd));
    return this;
  }

  public CredentialBuilder withProfile(ProfileBuilder builder){
    this.profileBuilder = builder;
    return this;
  }

  public Credential commit(){
    super.commit();
    Profile profile = profileBuilder
            .withCredential(product).commit();
    return product;
  }

  public static List<String> genSample(int size){
    ArrayList<String> result = new ArrayList<String>();
    for(int i = 0; i<size; i++){
      CredentialBuilder builder = new CredentialBuilder();
      builder.profileBuilder.withGender(i%2 == 0? Gender.MALE : Gender.FEMALE);
      Credential credential = builder.commit();
      result.add(credential.getUsername());
    }
    return result;
  }

  public static void main(String[] args) {
    List<Credential> allCredentials = new CredentialBuilder().list();
    for(Credential credential : allCredentials){
      logger.info(credential.toString());
    }
  }
}